package converters;

import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import modelo.Combobebida;
import services.ComboBebidaService;

@FacesConverter("combobebidaConverter")
public class ComboBebidaConverter implements Converter {
	
	public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
        	ComboBebidaService service = (ComboBebidaService) fc.getExternalContext().getApplicationMap().get("comboBebidaService");
            Iterator<Combobebida> i=service.getListaComboBebida().iterator();
            while (i.hasNext()) {
            	Combobebida cb = (Combobebida) i.next();
				if(cb.getIdComboBebida() == Integer.parseInt(value))
					return cb;
			}
            return null;
        }
        else {
            return null;
        }
    }
 
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
        	return String.valueOf(((Combobebida) object).getIdComboBebida());
        }
        else {
            return null;
        }
    }  

}
